// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import TushareModule from "../../module/TushareModule";

const {ccclass, property} = cc._decorator;

export var PlateEnumType = cc.Enum({
    Industry: 0,        //一级行业
    Subtrade: 1,        //细分行业
    Concept: 2,           //概念
});

export var ThsPlate = {
    Industry: 'industry',
    Subtrade: 'subtrade',
    Concept: 'concept',
};

export var PlateList = [ThsPlate.Industry, ThsPlate.Subtrade, ThsPlate.Concept];

export var PlateNames = ['行业', '细分行业', '概念'];

var PlateMapKeyList = ['thsIndustryMap', 'thsSubtradeMap', 'thsConceptMap'];

@ccclass
export class ThsRankData  {

    public static _instance: ThsRankData = null;

    private _rankMap: Object = {};

    private _finish: boolean = false;

    static getInstance(): ThsRankData {
        if (ThsRankData._instance == null) {
            ThsRankData._instance = new ThsRankData();
            ThsRankData._instance.initModule();
        }
        return ThsRankData._instance;
    }

    initModule () {

    }
    
    setDataCount (data) {
        for (let index = 0; index < PlateList.length; index++) {
            const key = PlateList[index];
            this[key+ 'Count'] = data[key+ 'Count'];
        }
        this._finish = false;
    }

    pushData (data) {
        for (let j = 0; j < PlateList.length; j++) {
            const key = PlateList[j];
            let keyData: any = data[key];
            let date = data.date;
            for (let index = 0; index < keyData.length; index++) {
                const item = keyData[index];
                let ts_code = item.ts_code;
                if (item.change == null) {
                    continue;
                }
                let msg = {
                    date: date, 
                    ts_code: ts_code, 
                    name: item.name, 
                    rank: index + 1, 
                    change: item.change,
                    volume: item.vol,
                    open: item.open,
                    close: item.close,
                    high: item.high,
                    low: item.low,
                    amount: item.amount,
                    turnrate: item.turnrate,
                    total_mv: item.total_mv,
                    float_mv: item.float_mv,
                    type: key,
                    plateType: j,
                };
                if (this._rankMap[ts_code] == null) {
                    this._rankMap[ts_code] = [];
                }
                this._rankMap[ts_code].push(msg);
            }
        }
    }

    getRankData (ts_code: string) {
        let list = this._rankMap[ts_code].slice();
        let item = list[0];
        let msg = {
            maxRank: this[item.type + 'Count'],
            list: list.reverse(),
            name: item.name,
        };
        return msg;
    }

    getPlateStocks (ts_code: string) {
        let module = TushareModule.getInstance();
        let stocks = module.thsPlateMember[ts_code];
        if (stocks == null) {
            return [];
        }
        for (let index = 0; index < stocks.length; index++) {
            const item = stocks[index];
            let market = module.dailyMarket[item.ts_code];
            item.market = market;
        }
        return stocks;
    }
	
	getPlatesStocks (plates) {
		let module = TushareModule.getInstance();
		let stockMap = {};
		for (let index = 0; index < plates.length; index++) {
            const ts_code = plates[index];
            let stocks = this.getPlateStocks(ts_code);
			for (let index = 0; index < stocks.length; index++) {
				const item = stocks[index];
				if (stockMap[item.ts_code] == null) {
					stockMap[item.ts_code] = item;
					let market = module.dailyMarket[item.ts_code];
					item.market = market;
				}
			}
        }

        let results = [];
        for (const code in stockMap) {
            if (Object.prototype.hasOwnProperty.call(stockMap, code)) {
                const item = stockMap[code];
                results.push(item);
            }
        }

        return results;
	}

    async getPlateAmounts (type, date) {
        let key = PlateList[type];
        let ret = await TushareModule.getInstance().getThsPlateAmounts(key, date);
        let amounts = [];
        for (let index = 0; index < ret.length; index++) {
            const item = ret[index];
            let data = {ts_code: item.ts_code, name: item.name, amount: item.amount, total: item.total_mv, rate: item.turnrate};
            amounts.push(data);
        }
        return amounts;
    }

    getPlateNodeInfo (ts_code) {
        let list = this._rankMap[ts_code];
        let item = list[0];
        let msg = {
            ts_code: ts_code,
            plateType: item.plateType,
            name: item.name,
        };
        return msg;
    }

    getPlatelist (type) {
        let list = [];
        let key = PlateMapKeyList[type];
        let map = TushareModule.getInstance()[key];
        for (const key in map) {
            if (Object.prototype.hasOwnProperty.call(map, key)) {
                const item = map[key];
                item.plateType = type;
                list.push(item);
            }
        }
        return list;
    }

    async getPlateDaysData (tradeCount) {
        
    }

    clearData () {
        this._rankMap = {};
    }

    finish () {
        this._finish = true;
    }

    isFinish () {
        return this._finish;
    }
}
